<?php
declare(strict_types = 1);

namespace App\Controller;

use App\Service\UserService;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Contract\RequestInterface;

/**
 * @AutoController()
 */
class ConstructInjectController
{
    /**
     * @var UserService
     */
    private $userService;

    // 在构造函数声明参数的类型，Hyperf 会自动注入对应的对象或值
    public function __construct(UserService $userService)
    {
        $this->userService = $userService;
    }

    // Hyperf 会自动为此方法生成一个 /construct_inject/info 的路由，允许通过 GET 或 POST 方式请求
    public function info(RequestInterface $request)
    {
        $id = $request->input('id', 1);
        return $this->userService->getInfoById((int)$id);
    }

}